# -*- coding: utf-8 -*-
import time
import math
import quantities

from abstract_sensor.definition import VTKSmartAbstractSensor
from abstract_node.output_interfaces import OutputInterfaceToConsole
        
class MySmartAbstractCamera(VTKSmartAbstractSensor):

    def __init__(self, outputDataSheet, sensor_interface, renderer, appName, outputLayer=[], logger=[]):
        VTKSmartAbstractSensor.__init__(self, 
                                     outputDataSheet=outputDataSheet,
                                     sensor_interface=sensor_interface,
                                     renderer=renderer,
                                     appName = appName)
        
    def input_event_mapping(self, value, time=[]):
        self.input_layer.input_event.set_value(value*quantities.centimeter, 'Measurement')
        self.input_layer.input_event.set_value([0.5]*quantities.centimeter**2, 'Uncertainty')   
      
    def output_event_mapping(self, results, common_validity):
        meas=self.input_events[0].get_value('Measurement')
        self.output_event.set_value(meas, 'Measurement')
        self.output_event.set_value(5.0*quantities.second, 'Timestamp')
        self.output_event.set_value([0.5]*quantities.centimeter**2, 'Uncertainty')
        self.output_event.set_value([100, 200, 0]*quantities.centimeter, 'Position')
        self.output_event.set_value([100, 200, 0]*quantities.degree, 'Orientation') 
        self.output_event.set_value([common_validity], 'Validity')  

class MySmartAbstractIRSensor(VTKSmartAbstractSensor):

    def __init__(self, outputDataSheet, sensor_interface, renderer, appName, outputLayer=[], logger=[]):
        VTKSmartAbstractSensor.__init__(self, 
                                     outputDataSheet=outputDataSheet,
                                     sensor_interface=sensor_interface,
                                     renderer=renderer,
                                     appName = appName)

    def input_event_mapping(self, value, time=[]):
        print value
        self.input_layer.input_event.set_value(value*quantities.centimeter, 'Measurement')
        self.input_layer.input_event.set_value([0.5]*quantities.centimeter**2, 'Uncertainty') 
        
    def event_managing(self, individualvalidity, value):
        meas=self.input_events[0].get_value('Measurement')
        self.event.set_value(meas, 'Measurement')
        self.event.set_value(1*quantities.cm**2,'Uncertainty')
        self.event.set_value((time.time()-self.start_time)*quantities.s, 'Timestamp')
        self.event.set_value(validity, 'Validity')
        self.event.set_value([100,100,100]*quantities.cm,'Position')
        self.event.set_value([100,100,100]*quantities.degree,'Orientation')
        #self.logger.info('Camera measurement event transmitted')


class MySmartAbstractTemp(VTKSmartAbstractSensor):
    """
     # PyUML: Do not remove this line! # XMI_ID:_frBiYPP1Ed-659Iqxr4BCA
    """

    def __init__(self, sensorInterface, eventDataSheet, outputfct,
                 appName, logger=[]):
        
        self.logger=logger
        self.start_time=time.time()
        vizSmartAbstractSensor.__init__(self,
                                     sensor_interface=sensorInterface,
                                     eventDataSheet=eventDataSheet,
                                     outputfct=[],
                                     appName=appName)  
        self.dispatcher.set_processingfct([self.intervallCheck])
        self.dispatcher.set_evaluationfct(self.event_managing)
        self.startMeasurements()
        
    def intervallCheck(self, value):
        result=0
        if value>0*quantities.degree and \
           value<50*quantities.degree: 
            result=1
        return result
    
    def validityCalculator(self, individualvalidity):
        return 1
        
    def event_managing(self, individualvalidity, value):
        validity=self.validityCalculator(individualvalidity)
        self.event.set_value(value, 'Measurement')
        self.event.set_value(1*quantities.cm**2,'Uncertainty')
        self.event.set_value((time.time()-self.start_time)*quantities.s, 'Timestamp')
        self.event.set_value(validity, 'Validity')
        self.event.set_value([100,100,100]*quantities.cm,'Position')


